package com.dqhl.community.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by cheng on 2016/8/30.
 * 记录activity
 */
public class RecordActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivTopBack;
    private TextView tvTopTitle;

    private LinearLayout llRecordPay; //缴费记录
    private LinearLayout llRecordRepair; //报修记录
    private LinearLayout llRecordComplaint; //投诉记录
    private LinearLayout llRecordOrdered; //预约记录

    private TextView tv_record_pay_number; //缴费记录数
    private TextView tv_record_repair_number; //报修记录数
    private TextView tv_record_complaint_number; //投诉记录数
    private TextView tv_record_ordered_number; //预约记录数

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        initView();
        getRecordNumber();
    }

    /**
     * 获取记录数
     */
    private void getRecordNumber() {
        RequestParams params = new RequestParams(Config.Api.recordNumber);
        params.addBodyParameter("userid", spUserInfo.getString(Constant.USERID, ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                Dlog.e("data:" + data);
                if (errCode == 0) {
                    String payNumber = JsonUtils.getInfo(data, "chargesOrder");
                    String repairNumber = JsonUtils.getInfo(data, "repair");
                    String complaintNumber = JsonUtils.getInfo(data, "complain");
                    String orderedNumber = JsonUtils.getInfo(data, "order");
                    tv_record_pay_number.setText(payNumber);
                    tv_record_repair_number.setText(repairNumber);
                    tv_record_complaint_number.setText(complaintNumber);
                    tv_record_ordered_number.setText(orderedNumber);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.txt_record);

        llRecordPay = (LinearLayout) findViewById(R.id.ll_record_pay);
        llRecordRepair = (LinearLayout) findViewById(R.id.ll_record_repair);
        llRecordComplaint = (LinearLayout) findViewById(R.id.ll_record_complaint);
        llRecordOrdered = (LinearLayout) findViewById(R.id.ll_record_ordered);

        tv_record_pay_number = (TextView) findViewById(R.id.tv_record_pay_number);
        tv_record_repair_number = (TextView) findViewById(R.id.tv_record_repair_number);
        tv_record_complaint_number = (TextView) findViewById(R.id.tv_record_complaint_number);
        tv_record_ordered_number = (TextView) findViewById(R.id.tv_record_ordered_number);

        ivTopBack.setOnClickListener(this);
        llRecordPay.setOnClickListener(this);
        llRecordRepair.setOnClickListener(this);
        llRecordComplaint.setOnClickListener(this);
        llRecordOrdered.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.ll_record_pay: // 缴费记录
                if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(PayRecordActivity.class);
                }
                break;
            case R.id.ll_record_repair: // 报修记录
                if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(RepairRecordActivity.class);
                }
                break;
            case R.id.ll_record_complaint: // 投诉记录
                if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(ComplainRecordActivity.class);
                }
                break;
            case R.id.ll_record_ordered: // 预约记录
                overlay(OrderedRecordActivity.class);
                break;
            default:
                break;
        }
    }
}
